package com.rchat.pocmini.activity.addfriend;

import android.app.AlertDialog;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SearchView;

import com.google.zxing.client.android.CaptureActivity;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.adapter.SearchUserResultAdapter;
import com.rchat.pocmini.activity.bean.FriendBean;
import com.rchat.pocmini.bean.SearchUserBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/8/7.
 *
 */

public class AddFriendMvpActivity extends BaseMvpActivity<AddFriendPresenterImpl, AddFriendModelImpl>
        implements AddFriendContract.View{

    @BindView(R.id.iv_back)
    ImageView mIvBack;

    @BindView(R.id.sv_search_friend)
    SearchView mSearchView;

    @BindView(R.id.pb)
    ProgressBar mPb;

    @BindView(R.id.rl_add_friend_from_scan)
    RelativeLayout mRlAddFriendFromScan;

    private AlertDialog dialog;


    private ArrayList<FriendBean> friendList = new ArrayList<>();

    private static final int SCAN_FRIEND_CODE = 1;



    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.add_friend_activity_layout;
    }

    @Override
    public void initView() {
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String content) {
                mPresenter.searchUser(content,content,content);
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void doAfterInitView() {

    }

    @OnClick({R.id.iv_back,
              R.id.rl_add_friend_from_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_add_friend_from_scan:
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, SCAN_FRIEND_CODE);
                break;
        }
    }

    @Override
    public void showSearchFriendResultDialog(SearchUserBean searchGroupBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View layout = inflater.inflate(R.layout.search_user_result_list_view_layout, null);//获取自定义布局  
        ListView lvResult = (ListView) layout.findViewById(R.id.search_user_result_list_view);

        SearchUserResultAdapter adapter = new SearchUserResultAdapter(this, searchGroupBean.getData().getUsers().getUser());
        lvResult.setAdapter(adapter);
        builder.setView(layout);
        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void dismissSearchUserDialog() {
        if(dialog!=null&&dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCAN_FRIEND_CODE:
                if (resultCode == RESULT_OK) {

                    mPresenter.scanUser(data);

                    //创建json
//                    String requestJson = createScanUserJson(userName);
//                    startSearchGroupThread(requestJson, UrlsGlobal.getUrl(UrlsGlobal.SCAN_FRIEND));
                }
                break;
            default:
                break;
        }
    }
}
